// pages/add/success.js
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
     data: {
          img_height: 0,
          data: {},
          width: 0,
          height: 0,
          canvasHeight:0
     },

	/**
	 * 生命周期函数--监听页面加载
	 */
     onLoad: function (options) {
          let that = this;
          var query = wx.createSelectorQuery()
          query.select('.success_back').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function (res) {
               that.setData({
                    img_height: res[0].width * 0.786
               });
          })

          var content_id = options.content_id;
          //登录
          app.login(function (userInfo) {
               that.setData({
                    userInfo: userInfo.data
               })

               //加载数据
               app.connect('content/detail', { content_id, content_id }, function (options) {
                    that.setData({
                         data: options
                    });
                    if (that.data.data.imgs.length == 0) {
                         console.log('一次不够，再次执行');
                         setTimeout(function () {
                              that.onLoad({ content_id: content_id });
                         }, 500);

                    }
                    //加载首图
                    wx.getImageInfo({
                         src: that.data.data.imgs[0].pic_path,
                         complete: function (res) {
                              that.setData({
                                   width: wx.getSystemInfoSync().screenWidth,
                                   height: wx.getSystemInfoSync().screenHeight
                              });
                              var width = that.data.width;
                              var height = that.data.height;
                              //图 和 框的比例
                              var pi = (res.width / res.height) / (width / height);
                              var pi1 = height / res.height;

                              //首图的真实高度
                              var trueHeght = res.height * pi1 / pi;
                              that.setData({
                                   canvasHeight:trueHeght + 130
                              });

                              //加载小程序码
                              wx.getImageInfo({
                                   src: app.globalData.siteroot + '/upload/qrcode/' + content_id + '.png',
                                   complete: function (qrcode) {
                                        //生成canvas
                                        var ctx = wx.createCanvasContext('canvas')
                                   
                                        ctx.save()
                                        ctx.setFillStyle('white')
                                        ctx.save(); // 先保存状态 已便于画完圆再用
                                        //先画底图
                                        that.roundRect2(ctx, 0, 0, width, trueHeght + 130, 10, true, '#000')
                                        ctx.fillRect(0, 0, width, trueHeght + 130);
                                        
                                       
                                        //画完底图画图集首图
                                        if (res.path != undefined) {
                                             ctx.drawImage(res.path, 0, 0, width, trueHeght)
                                        }
                                        //文字1
                                        ctx.setFillStyle('#303030')
                                        ctx.setFontSize(14)
                                        ctx.fillText('难道还要我教你怎么泡我吗？', width * 0.04 + 10, trueHeght + 30)
                                        ctx.fillText('还不赶快扫码～', width * 0.04 + 10, trueHeght + 48)

                                        //文字2
                                        ctx.setFillStyle('#BABABA')
                                        ctx.setFontSize(12)
                                        ctx.fillText('长按识或微信扫一扫小程序码，', width * 0.04 + 10, trueHeght + 90)
                                        ctx.fillText('看看Ta的私房照', width * 0.04 + 10, trueHeght + 108)

                                        //小程序码
                                        if (qrcode.path != undefined) {
                                             ctx.drawImage(qrcode.path, width  - 15 - width * 0.24, trueHeght + 18, width * 0.24, width * 0.24)
                                        }


                                        ctx.draw()
                                   }
                              })
                         }
                    })




               }, true);

          })



     },

	/**
	 * 用户点击右上角分享
	 */
     onShareAppMessage: function () {

     },
	/**
	 * 点击申请
	 */
     shenqing: function (e) {
          let that = this;
          app.connect('account/shenqing', {
               uid: that.data.userInfo.uid,
               openid: that.data.userInfo.openId,
               content_id: that.data.data.id
          }, function (options) {
               //加载数据
               app.connect('content/detail', { content_id: that.data.data.id }, function (options) {
                    that.setData({
                         data: options
                    });
               });
          }, true);
     },

     roundRect2: function (ctx, x, y, w, h, r, stroke, color) {
          ctx.save();
          ctx.beginPath()
          if (stroke) {
               ctx.setStrokeStyle(color);
          } else {
               ctx.setFillStyle(color);
          }
          x += 0.5;
          y += 0.5;
          ctx.setGlobalAlpha(0)
          ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);

          ctx.moveTo(x + r, y);

          ctx.lineTo(x + w - r, y);

          ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);

          ctx.lineTo(x + w, y + h - r);

          ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);

          ctx.lineTo(x + r, y + h);

          ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);

          ctx.lineTo(x, y + r);

          if (stroke) {
               ctx.stroke();
          } else {
               ctx.fill();
          };
          ctx.clip();
          ctx.setGlobalAlpha(1)

     },
     saveImg: function () {
          let that = this;
          //保存临时图片
          wx.canvasToTempFilePath({
               x: 0,
               y: 0,
               canvasId: 'canvas',
               quality: 1,
               success: function (res) {
                    // console.log("get tempfilepath(success) is:", res.tempFilePath)
                    //将图片保存在系统相册中(应先获取权限，)
                    wx.saveImageToPhotosAlbum({
                         filePath: res.tempFilePath,
                         success(res) {
                              wx.showToast({
                                   icon: 'success',
                                   title: '图片保存成功',
                              })
                              console.log("save photo is success")
                         },
                         fail: function (e) {
                              wx.showToast({
                                   title: '请授权后再试,如果还不能保存请到设置页打开授权',
                                   icon:'none'
                              })
                              wx.authorize({
                                   scope: 'scope.writePhotosAlbum',
                                   success() {
                                        
                                   }
                              })
                              console.log("save photo is fail" )
                         }
                    })
               },
               fail: function (e) {
                    console.log(e)
               }
          })
     },
     //转发
     onShareAppMessage: function (res) {
          let that = this;
          var data = that.data.data;
          console.log(that.data.data);
          return {
               title: '@' + data.nick + ' 在麦颜值发表的作品',
               path: 'pages/look/index?content_id=' + data.id + '&type=1',
               imageUrl: data.imgUrl
          }
     },
})